<?php


namespace app\api\controller;


use app\common\controller\Api;

class Goods extends Api
{
    protected $noNeedLogin = ['getPageList','getDetail'];
    protected $noNeedRight = ['*'];


    /**
     * 商品分页列表
     * @ApiMethod POST
     *
     * @param int $cate_id 分类
     * @param int $is_index 爆款推荐:0=否,1=是
     * @param string $sort_field none=无,all=综合(销量+浏览量),hot=热度(浏览量),price=价格
     * @param string $sort asc=正序,desc=倒序
     * @param string $search 搜索内容
     * @param int $page
     * @param int $limit
     */
    public function getPageList(\app\common\model\goods\GoodsModel $GoodsModel, \app\api\validate\GoodsValidate $GoodsValidate)
    {
        $params = $this->request->post();
        if(!$GoodsValidate->check($params,[],'getPageList')){
            $this->errorReturn(40001,$params,$GoodsValidate->getError());
        }
        $page = $this->request->request('page',1);
        $limit = $this->request->request('limit',5);
        $user_id = $this->auth->id;
        $return = $GoodsModel->getPageList($params,$user_id,$page,$limit);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }


    /**
     * 商品详情
     * @ApiMethod POST
     *
     * @param int $goods_id
     */
    public function getDetail(\app\common\model\goods\GoodsModel $GoodsModel, \app\api\validate\GoodsValidate $GoodsValidate)
    {
        $params = $this->request->get();
        if(!$GoodsValidate->check($params,[],'getDetail')){
            $this->errorReturn(40001,$params,$GoodsValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $GoodsModel->getDetail($params,$user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }


    /**
     * 商品搜索历史列表
     * @ApiMethod GET
     */
    public function getSearchList(\app\common\model\goods\GoodsSearchLogModel $GoodsSearchLogModel)
    {
        $user_id = $this->auth->id;
        $return = $GoodsSearchLogModel->getSearchList($user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }


    /**
     * 清空商品搜索历史列表
     * @ApiMethod GET
     */
    public function clearSearchLog(\app\common\model\goods\GoodsSearchLogModel $GoodsSearchLogModel)
    {
        $user_id = $this->auth->id;
        $return = $GoodsSearchLogModel->clearSearchLog($user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }


}